package it.uniroma3.dia.digraph.algorithm.utility.convexhull;

//newPoint.java
//
//Mark F. Hulber
//May 1996
//
//
//newPoint is an extension of the Java Point class.  Additions include 
// methods for making comparisons between points including relative 
// direction, magnitude, and angular computations.
//
//

import java.awt.geom.Point2D;

public class NewPoint extends Point2D.Double {
/**
	 * 
	 */
	private static final long serialVersionUID = 80941685846149377L;
public NewPoint(double nx, double ny){
   super();
   this.setLocation(nx, ny);
} 
public double length() {
   return Math.sqrt(this.getX()*this.getX() + this.getY()*this.getY());
} 
public int classify(NewPoint p0, NewPoint p1) {

  NewPoint a = new NewPoint(p1.getX()-p0.getX(), p1.getY()-p0.getY());
  NewPoint b = new NewPoint(this.getX() - p0.getX(), this.getY() - p0.getY());
  
  double sa = a.getX() * b.getY() - b.getX() * a.getY();

  if (sa > 0.0)
     return 0;  // LEFT
  if (sa < 0.0)
     return 1;  // RIGHT
  if ((a.getX() * b.getX() < 0.0) || (a.getY() * b.getY() < 0.0))
     return 2;  // BEHIND
  if (a.length() < b.length())
     return 3;  // BEYOND
  if (p0.equals(this))
     return 4;  // ORIGIN
  if (p1.equals(this))
     return 5;  // DESTINATION
  return 6;     // BETWEEN
}

public double polarAngle() {
  
  if ((this.getX() == 0.0) && (this.getY() == 0.0))
     return -1.0;
  if (this.getX() == 0.0)
    return ((this.getY() > 0.0) ? 90 : 270);
  double theta = Math.atan(this.getY()/this.getX());
  theta *= 360/(2*Math.PI);
  if (this.getX() > 0.0)
    return ((this.getY() >= 0.0) ? theta : 360 + theta);
  else
    return (180+theta);
}   



public int polarCmp(NewPoint p, NewPoint q) {


  NewPoint vp = new NewPoint(p.getX()-this.getX(), p.getY()-this.getY());
  NewPoint vq = new NewPoint(q.getX()-this.getX(), q.getY()-this.getY());

  double pPolar = vp.polarAngle();
  double qPolar = vq.polarAngle();

  if (pPolar < qPolar) return -1;
  if (pPolar > qPolar) return 1;
  if (vp.length() < vq.length()) return -1;
  if (vp.length() > vq.length()) return 1;
  return 0;
}

}
